﻿using Android.App;
using Android.Graphics;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System.Collections.Generic;

namespace WSYmi.MainAct
{
    /// <summary>
    /// 文件名列表定制适配器
    /// </summary>
    public class FileNameAdapter : BaseAdapter<CsvFileInfo>
    {
        private readonly List<CsvFileInfo> Items;

        private readonly Activity Context;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context">ListView所在Activity</param>
        /// <param name="items">要在列表中显示的项目</param>
        public FileNameAdapter(Activity context, List<CsvFileInfo> items) : base()
        {
            Context = context;
            Items = items;
        }

        public override CsvFileInfo this[int position]
        {
            get { return Items[position]; }
        }

        public override int Count
        {
            get { return Items.Count; }
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var item = Items[position];
            View view = convertView;
            view ??= Context.LayoutInflater.Inflate(Resource.Layout.FileName_item, null);

            //项目属性赋予控件.Text
            string collecting = "";
            TextView TvFileNmae = view.FindViewById<TextView>(Resource.Id.TvFileName);
            if (position == 0 && CsvFileInfo.IsCollecting)
            {
                collecting = "  [采集中]";
                TvFileNmae.Typeface = Typeface.Create((Typeface)null, TypefaceStyle.BoldItalic);
            }
            else
            {
                TvFileNmae.Typeface = Typeface.Create((Typeface)null, TypefaceStyle.Normal);
            }
            view.FindViewById<TextView>(Resource.Id.TvSn).Text = item.GetSn(position + 1);
            TvFileNmae.Text = item.Name + collecting;
            view.FindViewById<TextView>(Resource.Id.TvDeviceSn).Text = item.DeviceSn;
            view.FindViewById<TextView>(Resource.Id.TvTime).Text = item.Time;

            return view;
        }
    }

    /// <summary>
    /// 与ListViewAdapter配套的文件信息类
    /// </summary>
    public class CsvFileInfo
    {
        /// <summary>
        /// 第一个文件是否在采集中
        /// </summary>
        public static bool IsCollecting { get; set; } = false;

        /// <summary>
        /// 名称，文件名的一部分
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 保存时间
        /// </summary>
        public string Time { get; set; }

        /// <summary>
        /// 文件全名，文件读取时用到
        /// </summary>
        public string FileName { get; set; }

        /// <summary>
        /// 设备序列号
        /// </summary>
        public string DeviceSn { get; set; }

        public int DescribeContents()
        {
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// 列表序号，小于3位时加空格补齐
        /// </summary>
        /// <param name="sn">位置序号，从1开始</param>
        /// <returns></returns>
        public string GetSn(int sn)
        {
            string retSn = sn.ToString();
            return retSn;
        }
    }
}